 /** calculates checksum of NMEA message and compares it
     * 
     * 
     * @param msg
     * @return true if the checksum is valid, false if it is invalid
     */
    private boolean check(String msg) {

        boolean IAmValid = false;

        //calculate message length and subtract one (the carriage return)
        int msglen = msg.length() - 1;

        //if the message length is greater than 4 characters
        if (msglen > 4) {
            //check if there is a checksum in this message by seeing where the asterisk is
            //(it should be at the third last position in the message)
            if (msg.charAt(msglen - 3) == '*') {
                //  System.out.println("    received checksum : " + msg.substring(msglen - 2, msglen));
                // perform NMEA checksum calculation
                String chk_s = GetCheckSum(msg.substring(0, msglen - 3));

                // compare checksum to encoded checksum in msg
                IAmValid = msg.substring(msglen - 2, msglen).equals(chk_s);


                return (IAmValid);
            } else {
                // message doesn't have a checksum: do not accept it
                //   System.out.println("    message has no checksum.");
                invalidchecks++;
                return false;
            }
        }
        //(if the message length is greater than 4 characters)
        // don't accept messages without checksum
        return false;
    }


   /**
     * Trims the checksum off an NMEA message, then
     * recalculates the checksum
     * to compare it with the one passed along with the message later
     * 
     * @param msg String containing the full NMEA message (including checksum)
     * @return String containing the checksum
     */
    private static String GetCheckSum(String msg) {
        // perform NMEA checksum calculation
        int chk = 0;
        //run through each character of the message length
        //and XOR the value of chk with the byte value
        //of the character that is being evaluated
        for (int i = 1; i < msg.length(); i++) {
            chk ^= msg.charAt(i);
        }

        //convert the retreived integer to a HexString in uppercase
        String chk_s = Integer.toHexString(chk).toUpperCase();

        // checksum must be 2 characters!
        // if it falls short, add a zero before the checksum
        while (chk_s.length() < 2) {
            chk_s = "0" + chk_s;
        }

        //show the calculated checksum
        // System.out.println("    calculated checksum : " + chk_s);

        //return the calculated checksum
        return chk_s;
    }
